﻿using FlexFlow.Model;
using FlexFlow.Base.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace FlexFlow.DataProvider.MongoDB
{
    /// <summary>
    /// 提供通知提供器的MongoDb实现
    /// </summary>
    public class MongoDbNotificationProvider : MongoDBPhysicalModelProvider<Notification>, INotificationProvider
    {
        /// <summary>
        /// 使用默认设置创建通知
        /// </summary>
        /// <returns>
        /// 创建的通知
        /// </returns>
        public override Notification Create()
        {
            return new Notification()
            {
                CreateTime = DateTime.Now
            };
        }
        /// <summary>
        /// 取得指定目录中从指定日期开始的消息数量
        /// </summary>
        /// <param name="catalog">
        /// 消息目录ID
        /// </param>
        /// <param name="fromDate">
        /// 消息开始日期
        /// </param>
        /// <returns>
        /// 消息数量
        /// </returns>
        public int Count(Guid catalog, DateTime fromDate)
        {
            FilterDefinition<Notification> filterDef = (FilterDefinition<Notification>)
                (m => m.Catalog == catalog && m.CreateTime > fromDate && m.Readed == false);
            var col = GetCollection();
            var countTask = col.CountAsync(filterDef);
            countTask.Wait();
            return (int)countTask.Result;
        }

        /// <summary>
        /// 取得指定用户指定日期后的通知列表
        /// </summary>
        /// <param name="uid">
        /// 用户ID
        /// </param>
        /// <param name="fromDate">
        /// 通知起始日期
        /// </param>
        /// <returns>
        /// 取得的通知列表
        /// </returns>
        public List<Notification> Get(Guid uid, DateTime fromDate)
        {
            FilterDefinition<Notification> filterDef = (FilterDefinition<Notification>)(m => m.Reciver == uid && m.CreateTime > fromDate);
            var col = GetCollection();
            return col.GetAll(filterDef);
        }
    }
}
